import dt from '@dt/dt'
import buildingApi from '@/config/server/api/building.js'
import customerApi from '@/config/server/api/customer.js'
import userApi from '@/config/server/api/user.js'
import fitClassApi from '@/config/server/api/fitClass.js'
import baseApi from '@/config/server/api/base.js'
import biApi from '@/config/server/api/bi.js'

let consultantsList = [],
  intentionProductList = [],
  statusList = [],
  intentionHouseTypeList = [],
  intentionAreaList = [],
  intentionTotalPriceList = [],
  intentionAveragePriceList = [],
  channelSourceList = [],
  intentionLevelList = [],
  intentionPurposeList = [],
  intentionRegionList = []

function columns(clue, type, disabledFlag = true) {
  let headApi = ''
  if (clue) {
    headApi = customerApi.clue.clueHeadList
  } else {
    headApi = customerApi.customer.customerHeadList
  }
  return headApi
    .fetch({
      buildingId: dt.buildInfo().housesId,
      type: type
    })
    .then((res) => {
      if (!clue) {
        res.showHead = res.showHead.filter((item) => {
          if (
            item.fieldKey != 'clueSourceChannel' &&
            item.fieldKey != 'firstContactDate' &&
            item.fieldKey != 'isEffective'
          ) {
            return item
          }
        })
      }
      let data = [],
        showHead = res.showHead,
        hiedHead = res.hiedHead

      res.showHead = res.showHead.map((item) => {
        item.disabled = false
        let obj = {
          field: item.fieldKey,
          title: item.fieldValue,
          minWidth: 150,
          maxWidth: 300,
          dbKey: item.dbKey,
          fieldValue: item.fieldValue
        }
        if (item.fieldKey == 'customerName') {
          // 客户姓名
          item.disabled = true
          data[0] = obj
        } else if (item.fieldKey == 'customerPhone') {
          // 手机号码
          item.disabled = true
          data[1] = obj
        } else if (item.fieldKey == 'attributionConsultant') {
          // 归属顾问
          item.disabled = disabledFlag
          data[2] = obj
        } else if (item.fieldKey == 'intentionProduct') {
          // 意向业态
          intentionProductList = item.options.map((i) => ({
            key: i.optionId,
            value: i.optionValue
          }))
        } else if (item.fieldKey == 'status') {
          // 客户进度
          statusList = item.options.map((i) => ({
            key: i.optionId,
            value: i.optionValue
          }))
        } else if (item.fieldKey == 'intentionHouseType') {
          // 意向户型
          intentionHouseTypeList = item.options.map((i) => ({
            key: i.optionId,
            value: i.optionValue
          }))
        } else if (item.fieldKey == 'intentionLevel') {
          // 意向等级
          intentionLevelList = item.options.map((i) => ({
            key: i.optionId,
            value: i.optionValue
          }))
        } else if (item.fieldKey == 'intentionPurpose') {
          // 置业目的
          intentionPurposeList = item.options.map((i) => ({
            key: i.optionId,
            value: i.optionValue
          }))
        }
        return obj
      })
      let tmp = {
        resArr: [
          ...data,
          ...res.showHead.filter(
            (i) =>
              i.field != 'customerName' && i.field != 'customerPhone' && i.field != 'attributionConsultant'
          )
        ],
        hiedHead: hiedHead,
        showHead: showHead
      }
      return tmp
    })
}

function allotColumns() {
  let headApi = customerApi.clue.clueHeadList
  return headApi
    .fetch({
      buildingId: dt.buildInfo().housesId
    })
    .then((res) => {
      let data = [],
        showHead = res.showHead,
        hiedHead = res.hiedHead
      res.showHead = res.showHead.map((item) => {
        item.disabled = false
        let obj = {
          field: item.fieldKey,
          title: item.fieldValue,
          minWidth: 150,
          maxWidth: 300,
          dbKey: item.dbKey,
          fieldValue: item.fieldValue
        }
        if (item.fieldKey == 'customerName') {
          // 客户姓名
          item.disabled = true
          data[0] = obj
        } else if (item.fieldKey == 'customerPhone') {
          // 手机号码
          item.disabled = true
          data[1] = obj
        } else if (item.fieldKey == 'attributionConsultant') {
          // 归属顾问
          item.disabled = true
          data[2] = obj
        } else if (item.fieldKey == 'intentionProduct') {
          // 意向业态
          intentionProductList = item.options.map((i) => ({
            key: i.optionId,
            value: i.optionValue
          }))
        } else if (item.fieldKey == 'status') {
          // 客户进度
          statusList = item.options.map((i) => ({
            key: i.optionId,
            value: i.optionValue
          }))
        } else if (item.fieldKey == 'intentionHouseType') {
          // 意向户型
          intentionHouseTypeList = item.options.map((i) => ({
            key: i.optionId,
            value: i.optionValue
          }))
        } else if (item.fieldKey == 'intentionLevel') {
          // 意向等级
          intentionLevelList = item.options.map((i) => ({
            key: i.optionId,
            value: i.optionValue
          }))
        } else if (item.fieldKey == 'intentionPurpose') {
          // 置业目的
          intentionPurposeList = item.options.map((i) => ({
            key: i.optionId,
            value: i.optionValue
          }))
        }
        return obj
      })
      let tmp = {
        resArr: [
          ...data,
          ...res.showHead.filter(
            (i) =>
              i.field != 'customerName' && i.field != 'customerPhone' && i.field != 'attributionConsultant'
          )
        ],
        hiedHead: hiedHead,
        showHead: showHead
      }
      return tmp
    })
}

function list(info) {
  return customerApi.clue.list
    .fetch({
      roleId: dt.userInfo().role.roleId,
      buildingId: dt.buildInfo().housesId,
      ...info
    })
    .then((res) => {
      res.list = res.records
      return res
    })
}

function count(info) {
  return customerApi.clue.count
    .fetch({
      roleId: dt.userInfo().role.roleId,
      buildingId: dt.buildInfo().housesId,
      ...info
    })
    .then((res) => {
      return res
    })
}

function exportData(info) {
  info.queryWebCustomerListDTO.buildingId = dt.buildInfo().housesId
  info.queryWebCustomerListDTO.roleId = dt.userInfo().role.roleId
  info.queryWebCustomerListDTO.page = 1
  info.queryWebCustomerListDTO.rows = 9999
  delete info.queryWebCustomerListDTO.current
  return customerApi.clue.export.fetch(info)
}

function distribution(info) {
  info.buildingId = dt.buildInfo().housesId
  return customerApi.clue.distribution.fetch(info)
}

function customerInfo(info) {
  return customerApi.clue.info.fetch(info)
}

function getConsultant(name) {
  return userApi.counselorList
    .fetch({
      buildingId: dt.buildInfo().housesId,
      userName: name || ''
    })
    .then((res) => {
      let data = res.map((item) => ({
        ...item,
        key: item.userId,
        value: item.userName
      }))
      consultantsList = data
      return data
    })
}

function adviserList(info) {
  return userApi.customer.adviserList
    .fetch({
      buildingId: dt.buildInfo().housesId,
      onlineType: 1,
      haveRole: 1
    })
    .then((res) => {
      return res
    })
}

function getFilterOptions() {
  return buildingApi.generalConfiguration.selectAggregationCriteria
    .fetch({
      cityCode: dt.buildInfo().cityId,
      housesId: dt.buildInfo().housesId
    })
    .then((res) => {
      intentionAreaList = res.areaList.map((i) => ({
        key: i.searchParamId,
        value: i.paramValues
      }))
      intentionTotalPriceList = res.totalList.map((i) => ({
        key: i.searchParamId,
        value: i.paramValues
      }))
      intentionAveragePriceList = res.priceList.map((i) => ({
        key: i.searchParamId,
        value: i.paramValues
      }))
      return res
    })
}

function getChannel() {
  return baseApi.caseChannel.caseChannelList
    .fetch({
      page: 1,
      rows: 100,
      cityCode: dt.buildInfo().cityId,
      buildingId: dt.buildInfo().housesId,
      userId: dt.userInfo().userId
    })
    .then((res) => {
      let data = res.list.map((i) => ({
        key: i.channelId,
        value: i.channelName
      }))
      channelSourceList = data
      return data
    })
}

function getArea() {
  return baseApi.city.cityBaseList
    .fetch({
      parentCode: dt.buildInfo().cityId,
      level: 4
    })
    .then((res) => {
      let data = res.list.map((i) => ({
        key: i.cityId,
        value: i.cityName
      }))
      intentionRegionList = data
      return data
    })
}

function follow(info) {
  info.buildingId = dt.buildInfo().housesId
  info.operatorId = dt.userInfo().userId
  return customerApi.customer.followUp.fetch(info)
}

function updateFollow(info) {
  info.buildingId = dt.buildInfo().housesId
  info.operatorId = dt.userInfo().userId
  return customerApi.customer.updateFollowUp.fetch(info)
}

function dealRange(obj, unit) {
  let str = ''
  if (obj.start == '' || obj.start == '0') {
    str = obj.end + unit + '以下'
  } else if (obj.end == '' || obj.end > 99999) {
    str = obj.start + unit + '以上'
  } else {
    str = obj.start + '~' + obj.end + unit
  }
  return str
}

function consultants() {
  return consultantsList
}

function intentionProduct() {
  return intentionProductList
}

function status() {
  return statusList
}

function intentionHouseType() {
  return intentionHouseTypeList
}

function intentionArea() {
  return intentionAreaList
}

function intentionTotalPrice() {
  return intentionTotalPriceList
}

function intentionAveragePrice() {
  return intentionAveragePriceList
}

function channelSource() {
  return channelSourceList
}

function intentionLevel() {
  return intentionLevelList
}

function intentionPurpose() {
  return intentionPurposeList
}

function intentionRegion() {
  return intentionRegionList
}

function customerBigHead(info) {
  return biApi.customer.customerBigHead.fetch(info)
}

function customerBigHeadUpdate(info) {
  return biApi.customer.customerBigHeadUpdate.fetch(info)
}

function largeCustomerList(info) {
  return biApi.customer.largeCustomerList.fetch(info)
}

function largeCustomerStatistics(info) {
  return biApi.customer.largeCustomerStatistics.fetch(info)
}

function largeCustomerExport(info) {
  return biApi.customer.largeCustomerExport.fetch(info)
}

function largeCustomerSummary(info) {
  return biApi.customer.largeCustomerSummary.fetch(info)
}

function planList(info) {
  return fitClassApi.planChannel.planList.fetch(info)
}

export default {
  columns,
  allotColumns,
  list,
  count,
  exportData,
  distribution,
  customerInfo,
  getConsultant,
  adviserList,
  getFilterOptions,
  getChannel,
  getArea,
  follow,
  updateFollow,

  consultants,
  intentionProduct,
  status,
  intentionHouseType,
  intentionArea,
  intentionTotalPrice,
  intentionAveragePrice,
  channelSource,
  intentionLevel,
  intentionPurpose,
  intentionRegion,
  customerBigHead,
  customerBigHeadUpdate,
  largeCustomerList,
  largeCustomerStatistics,
  largeCustomerExport,
  planList,
  largeCustomerSummary
}
